//
//  SXReadDetailListTableViewCell.m
//  ShiXun
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXReadDetailListTableViewCell.h"
#import "UIView+UIView.h"
#import "UIImageView+WebCache.h"

@implementation SXReadDetailListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _imgsrcView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80 * 490 / 364, 80)];
        [self.contentView addSubview:_imgsrcView];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_imgsrcView.width + 15, 10, kScreenWidth - _imgsrcView.width - 25, 60)];
        _titleLabel.numberOfLines = 3;
        _titleLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_titleLabel];
        
        _sourceLabel = [[UILabel alloc] initWithFrame:CGRectMake(_titleLabel.x, 78, _titleLabel.width, 10)];
        _sourceLabel.font = [UIFont systemFontOfSize:12];
        _sourceLabel.textColor = [UIColor colorWithWhite:0.344 alpha:1.000];
        [self.contentView addSubview:_sourceLabel];
    }
    return self;
}

- (void)setCongfigureModel:(SXReadModel *)congfigureModel
{
    _congfigureModel = congfigureModel;
    
    if (congfigureModel.imgsrc.length > 0)
    {
        _imgsrcView.frame = CGRectMake(10, 10, 80 * 490 / 364, 80);
        _titleLabel.frame = CGRectMake(_imgsrcView.width + 15, 10, kScreenWidth - _imgsrcView.width - 25, 60);
        _sourceLabel.frame = CGRectMake(_titleLabel.x, 78, _titleLabel.width, 10);
        [_imgsrcView sd_setImageWithURL:[NSURL URLWithString:congfigureModel.imgsrc] placeholderImage:[UIImage imageNamed:@"SX-placeholder"]];
        CGFloat titleH = [SXReadDetailListTableViewCell heightOfString:congfigureModel.title withWidht:kScreenWidth - _imgsrcView.width - 25 withFontSize:15];
        _titleLabel.height = titleH < 60 ? titleH : 60;
    }
    else{
        _imgsrcView.frame = CGRectZero;
        CGFloat titleH = [SXReadDetailListTableViewCell heightOfString:congfigureModel.title withWidht:kScreenWidth - 20 withFontSize:15];
        _titleLabel.height = titleH < 60 ? titleH : 60;
        _titleLabel.x = 10;
        _titleLabel.width = kScreenWidth - 20;
        
        _sourceLabel.frame = CGRectMake(10, _titleLabel.height + 20, kScreenWidth - 20, 10);
    }
    _titleLabel.text = congfigureModel.title;
    _sourceLabel.text = congfigureModel.source;
}

// 字符串高度
+ (CGFloat)heightOfString:(NSString *)str withWidht:(CGFloat)width withFontSize:(CGFloat)size
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:size] forKey:NSFontAttributeName];
    CGRect bounds = [str boundingRectWithSize:CGSizeMake(width, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return bounds.size.height;
}

+ (CGFloat)heightForCellWithModel:(SXReadModel *)model
{
    if (model.imgsrc.length > 0)
    {
        return 100;
    }
    else
    {
        return [SXReadDetailListTableViewCell heightOfString:model.title withWidht:kScreenWidth - 20 withFontSize:15] + 40;
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
